package ntrees.hu.renren;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PhotoGetResponseBean extends ResponseBean {

	private List<RenrenPhotoEntry> photos = new ArrayList<RenrenPhotoEntry>();

	public PhotoGetResponseBean() {
		super(null);
	}
	
	public PhotoGetResponseBean(String response) {
		this(response, "json");
	}

	/**
	 * 构造函数，通过请求返回的字符串构造返回对象
	 * 
	 * @param response
	 * @param format
	 */
	public PhotoGetResponseBean(String response, String format) {
		super(response);

		if (response == null) {
			return;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");

		if (format.toLowerCase().endsWith("json")) {
			try {
				JSONArray array = new JSONArray(response);
				int size = array.length();
				JSONObject object = null;
				for (int i = 0; i < size; i++) {
					object = array.optJSONObject(i);
					if (object != null) {
						RenrenPhotoEntry photo = new RenrenPhotoEntry();
						photo.urlTiny = (object.optString("url_tiny"));
						photo.urlHead = (object.optString("url_head"));
						photo.urlLarge = (object.optString("url_large"));
						photo.urlMain = (object.optString("url_main"));
						photo.createTime = sdf.parse(object.optString("time")).getTime();
						photo.caption = object.optString("caption");
						photo.pid = object.getLong("pid");
						photo.aid = object.getLong("aid");
						photo.uid = object.getLong("uid");
						photos.add(photo);
					}
				}
			} catch (JSONException e) {
				Util.logger("exception in parsing json data: " + e.getMessage());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

	}
	
	public List<RenrenPhotoEntry> getPhotos() {
		return photos;
	}
}
